<?php
//include_once "../config/config.php";
//include_once "Player.php";
//include_once "CardsDeck.php";
require_once(CARDS_DECK_FILE);
require_once(PLAYER_FILE);
require_once(UTILS_FILE);

class GameLogic
{
	private $_players;
	private $_inst;
	
	public function __construct($player_1, $player_2) {

		$this->_players[] = $player_1;
		$this->_players[] = $player_2;
		
		$this->_inst = CardsDeck::Instance();
	}
	
	public function place_card($player_id, $card_id, $x, $y) {
		foreach($this->_players as $player) {
			if($player->ID() == $player_id) {
				if(Utils::boundries_check($x, $y)) {
					return $player->place_card($card_id, $x, $y);
				}
				else {
					Utils::Error("Error - GameLogic::place_card - $x, $y not lagit cordinate!");
					return null;
				}
			}
		}
		Utils::Error("Error - GameLogic::place_card - no id exist!");
		return null;
	}
	
	public function attack_card($attacker_id, $row, $col) {
		$attacker = null;
		$defender = null;
		
		if($this->_players[0]->ID() == $attacker_id) {
			$attacker = $this->_players[0];
			$defender = $this->_players[1];
		}
		else if($this->_players[1]->ID() == $attacker_id) {
			$attacker = $this->_players[1];
			$defender = $this->_players[0];
		}
		else {
			Utils::Error("Error - GameLogic::attack_card - player $attacker_id is not a real player!");
			return null;
		}
		$placed = $attacker->cell($row, $col);
		if($placed == null) {
			Utils::Error("Error - GameLogic::attack_card - no card in $row, $col!");
			return null;
		}

		$dmg = $this->_inst->card_dmg($placed->ID());
		$defender->take_damage($dmg, $row);
		return $dmg;
	}
	
	public function draw_card($player_id) {
		foreach($this->_players as $player) {
			if($player->ID() == $player_id) {
				if($player->deck_not_full()) {
					return $player->draw_card();
				}
				else {
					Utils::Error("Error - GameLogic::draw_card - player $player_id deck is full!");
					return null;
				}
			}
		}
		Utils::Error("Error - GameLogic::draw_card - no id exist!");
		return null;
	}
	
	private function player_has_card($player_id, $card_id) {
		foreach($this->_players as $player) {
			if($player->ID() == $player_id) {
				return $player->has_card($card_id);
			}
		}
		Utils::Error("Error - GameLogic::player_has_card - no id exist!");
		return null;
	}
	
	private function row_full($player_id, $row) {
		foreach($this->_players as $player) {
			if($player->ID() == $player_id) {
				return $player->row_full($row);
			}
		}
		Utils::Error("Error - GameLogic::row_full - no id exist!");
		return null;
	}

	private function board_full($player_id) {
		foreach($this->_players as $player) {
			if($player->ID() == $player_id) {
				return $player->board_full();
			}
		}
		Utils::Error("Error - GameLogic::board_full - no id exist!");
		return null;
	}
	
	public function player_lost($player_id) {
		foreach($this->_players as $player) {
			if($player->ID() == $player_id) {
				return $player->is_lost();
			}
		}
		Utils::Error("Error - GameLogic::player_lost - no id exist!");
		return null;
	}
	
	public function return_answer() {
		$p1_id = $this->_players[0]->ID();
		$p2_id = $this->_players[1]->ID();
		$p1_board = $this->_players[0]->return_board_answer();
		$p2_board = $this->_players[1]->return_board_answer();
		$p1_towers = $this->_players[0]->return_towers_answer();
		$p2_towers = $this->_players[1]->return_towers_answer();
		$p1_cards = $this->_players[0]->return_cards_answer();
		$p2_cards = $this->_players[1]->return_cards_answer();
		
		$record['p1_id'] = $p1_id;
		$record['p2_id'] = $p2_id;
		$record['p1_board'] = $p1_board;
		$record['p2_board'] = $p2_board;
		$record['p1_towers'] = $p1_towers;
		$record['p2_towers'] = $p2_towers;
		$record['p1_cards'] = $p1_cards;
		$record['p2_cards'] = $p2_cards;
		
		return $record;
	}

	//getters
	public function getPlayerByID($id) {
		foreach($this->_players as $player) {
			if($player->ID() == $id) {
				return $player;
			}
		}
		Utils::Error("Error - GameLogic::getPlayerByID - id $id not exist!");
		return null;
	}
	public function getID() { return $this->_id; }
	public function getState() { return $this->_state; }
	
	public function has_won($uid) {
		if($this->_players[0]->ID() == $uid) {
			return $this->player_lost($this->_players[1]->ID());
		}
		else if($this->_players[1]->ID() == $uid) {
			return $this->player_lost($this->_players[0]->ID());
		}
		else {
			Utils::Error("uid not recognized");
			return null;
		}
	}
}

?>